package com.cuotigou.controller;

import com.cuotigou.config.ConfigUtils;
import com.cuotigou.mapper.SUserMapper;
import com.cuotigou.pojo.SUser;
import com.cuotigou.service.UserService;
import com.cuotigou.util.CommonUtils;
import com.cuotigou.util.ObjectResult;
import com.cuotigou.util.StringUtil;
import com.cuotigou.util.XMLBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/10/30.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private SUserMapper sUserMapper;

    /**
     * 注册
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult register(XMLBean xmlBean){
        if (StringUtil.isEmpty(xmlBean.getMobile()) ||StringUtil.isEmpty(xmlBean.getPassword())||StringUtil.isEmpty(xmlBean.getUsername())
        ||StringUtil.isEmpty(xmlBean.getGrade())||StringUtil.isEmpty(xmlBean.getCity())){
            return ObjectResult.build("0","参数丢失，请重试");
        }
        return userService.register(xmlBean);
    }

    /**
     * 登陆
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult login(XMLBean xmlBean){
        if (StringUtil.isEmpty(xmlBean.getMobile()) ||StringUtil.isEmpty(xmlBean.getPassword())){
            return ObjectResult.build("0","参数丢失，请重试");
        }
        return userService.login(xmlBean);
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult logout(){
        return ObjectResult.build("1","成功");
    }

    /**
     * 忘记密码
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult updatePassword(XMLBean xmlBean){
        if (StringUtil.isEmpty(xmlBean.getMobile()) ||StringUtil.isEmpty(xmlBean.getPassword())){
            return ObjectResult.build("0","参数丢失，请重试");
        }
       return userService.updatePassword(xmlBean);
    }

    /**
     * 用户信息
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/userInform", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult userInform(XMLBean xmlBean){
        if (StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","参数丢失，请重试");
        }
        String userId = CommonUtils.decToken(xmlBean.getToken());
        SUser sUser = sUserMapper.selectByPrimaryKey(Long.parseLong(userId));
        return  ObjectResult.build("1","成功",sUser);
    }


    /**
     * 修改个人信息
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/updateInform", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult updateInform(XMLBean xmlBean, HttpServletRequest request){
        if (StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        return userService.updateInform(xmlBean,request);
    }

    /**
     * vip页面
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/vip", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult vip(XMLBean xmlBean){
        if (StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        return userService.vip(xmlBean);
    }

    /**
     * 邀请页面
     * @param xmlBean
     * @return
     */
    @RequestMapping("/invite")
    @ResponseBody
    public ObjectResult invite(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        Map<String,Object> data = new HashMap<String,Object>();
        String userId = CommonUtils.decToken(xmlBean.getToken());

        data.put("title","错题狗在手，学习无忧。");
        data.put("content","学习就两步：1、找到不会的;2、把不会的变成会的;解题就是缩小自身思维能力和出题者要求之间的差距。");
        data.put("url", ConfigUtils.SYS_URL+"index/html?title=index");
        data.put("examine",ConfigUtils.EXAMINE);

        return ObjectResult.build("1","成功",data);
    }
}
